/**
 * @Company:中享思途   
 * @Title:FrontShopCarController.java 
 * @Author:IL Clown   
 * @Date:2020年8月25日 下午9:47:19     
 */
package com.situ.shopsystem.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.mysql.cj.xdevapi.UpdateStatement;
import com.situ.shopsystem.pojo.Address;
import com.situ.shopsystem.pojo.Order;
import com.situ.shopsystem.pojo.Product;
import com.situ.shopsystem.pojo.ShopCar;
import com.situ.shopsystem.pojo.User;
import com.situ.shopsystem.service.AddressService;
import com.situ.shopsystem.service.OrderService;
import com.situ.shopsystem.service.ProductService;

/** 
 * @ClassName:FrontShopCarController 
 * @Description:(这里用一句话描述这个类的作用)  
 * 
 * shop/doAddShopCar
 * shop/goShopCar
 * shop/deleteShopCar
 * shop/goOrder
 * shop/goFrontOrder
 * 
 * 
 */

@Controller
@RequestMapping("/shop/")
public class FrontShopCarController {
	@Autowired
	private ProductService productService;
	@Autowired
	private AddressService addressService;
	@Autowired
	private OrderService orderService;

	@RequestMapping("goShopCar")
	public String goShopCar(HttpSession session, Long id, Model model) {
		Map<Long, Long> map = (Map<Long, Long>) session.getAttribute("shopCar");
		List<ShopCar> list = new ArrayList<ShopCar>();
		double sum = 0;
		if (map != null) {
			for (Long key : map.keySet()) {
				Product product = productService.findOne(key);
				list.add(new ShopCar(product, map.get(key)));
				sum += product.getProPrice() * map.get(key);

			}
		}
		model.addAttribute("sum", sum);
		model.addAttribute("shopCar", list);

		return "shop/shopCar";
	}

	@RequestMapping("doAddShopCar")
	public String doAddShopCar(HttpSession session, Long id, Model model) {
		Map<Long, Long> map = (Map<Long, Long>) session.getAttribute("shopCar");
		if (map == null) {
			map = new HashMap<Long, Long>();

		}
		if (map.containsKey(id)) {
			map.put(id, map.get(id) + 1L);

		} else {
			map.put(id, 1L);

		}
		session.setAttribute("shopCar", map);

		return "redirect:goShopCar";

	}

	@RequestMapping("deleteShopCar")
	public String deleteShopCar(Long id, HttpSession session) {
		Map<Long, Long> map = (Map<Long, Long>) session.getAttribute("shopCar");
		map.remove(id);
		session.setAttribute("shopCar", map);

		return "redirect:goShopCar";
	}

	@RequestMapping("goOrder")
	public String goOrder(Model model, HttpSession session) {
		Map<Long, Long> map = (Map<Long, Long>) session.getAttribute("shopCar");
		List<ShopCar> list = new ArrayList<ShopCar>();
		User user = (User) session.getAttribute("user");
		List<Address> list2 = addressService.findAll(user.getRowId());
		double sum = 0;
		if (map != null) {
			for (Long key : map.keySet()) {
				Product product = productService.findOne(key);
				list.add(new ShopCar(product, map.get(key)));
				sum += product.getProPrice() * map.get(key);

			}
		}
		model.addAttribute("sum", sum);
		model.addAttribute("shopCar", list);
		model.addAttribute("address", list2);

		return "shop/confirm-order";
	}

	@RequestMapping("goFrontOrder")
	public String goFrontOrder(Model model, HttpSession session, Order order) {
		User user = (User) session.getAttribute("user");
		List<Order> list = orderService.findByUserId(user.getRowId());
		model.addAttribute("orderList", list);
		return "shop/order";
	}

	@RequestMapping("updateState")
	public String updateState(Long rowId) {
		orderService.updateState(rowId);
		return "redirect:goFrontOrder";
	}

	@RequestMapping("doAddOrder")
	public String doAddOrder(Order order, HttpSession session, Long addressId, double sum) {
		order.setAddressId(addressId);
		order.setPrice(sum);
		orderService.save(order, session);
		return "redirect:goFrontOrder";
	}

}
